package com.yunhoo.contract.bean;

import com.yunhoo.contract.db.impl.*;
import com.yunhoo.contract.model.*;
import com.yunhoo.sys.util.common.*;
import java.util.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import com.yunhoo.contract.util.DocumentQueryConditionBeanExecute;

/**
 * 合同执行
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class ContractExecuteBean {

    private ConExecMaster master = new ConExecMaster();//执行单基本信息
    private List<ConExecDetail> detailli = null;//执行单 执行内容
    private DataModel<ConExecDetail> detailModel = new ListDataModel<ConExecDetail>();
    //
    private Integer contentRowIndex = -1;//当前行 的 行号
    //
    private boolean addOrUpdate = false;
    private boolean editStatus = false;
    private boolean contractChanged = true;
    //合同执行单 列表
    private boolean querybut = true;
    private DataModel<ConExecMaster> execModel = null;
    private List<ConExecDetail> detailListForShow = null;
    //
    private DataModel<MainInformation> mainInfoModel = new ListDataModel<MainInformation>();//合同列表
    private DataModel<ContractContent> contentModel = new ListDataModel<ContractContent>();//合同内容
    //
    private MainInformation mainInfo = null;
    private List<ConExecDetail> allExecList = null;//合同下 所有执行单
    private String contype = "";//合同类型 0：主合同/1：补充合同

    public ContractExecuteBean() {
        //    initPrcProModel();
        initExecli();
    }

    //合同执行单 报表部分
    private void initExecli() {
        List<ConExecMaster> execli = ConExecImpl.queryExecuteMaster(null);
        execModel = new ListDataModel(execli);
    }

    public void contractQueryed() {
        contractChanged = false;
    }

    public void changeContract() {
        contractChanged = true;
        mainInfo = mainInfoModel.getRowData();
        master.setNumber(mainInfo.getNumber());
        master.setConName(mainInfo.getCname());
        
        initContentModel(mainInfo.getNumber());
    }

    public void query() {//合同执行单报表动态查询
        DocumentQueryConditionBeanExecute dqcb = (DocumentQueryConditionBeanExecute) FacesUtils.getManagedBean("queryExecBean");
        List<ConExecMaster> execli = ConExecImpl.queryExecuteMaster(dqcb);
        execModel.setWrappedData(execli);
    }

    public void execSelected() {//合同执行单 明细查看
        master = execModel.getRowData();
        mainInfo = master.getMainInfo();
        System.out.println(" * * * " + "number : " + mainInfo.getNumber() + "  * * * ");
        System.out.println(" * * * " + "cname : " + mainInfo.getCname()+ "  * * * ");
        
        initContentModel(mainInfo.getNumber());
        
        detailli = ConExecImpl.queryDetailByNumber(master.getVounumber().trim());
        detailModel.setWrappedData(detailli);
        
        initStatus();
        contractChanged = true;
        
        NavigationTool.getSelectedUrl("/page/contract/business/conexec.xhtml");
    }

    public void rowDetail() {
        master = execModel.getRowData();
        detailListForShow = ConExecImpl.queryDetailByNumber(master.getVounumber().trim());
    }

    public void queryCancel() {
        initExecli();
    }

    public void hiddenbut() {
        querybut = false;
    }

    public void showbut() {
        querybut = true;
    }

    public void goback() {
        NavigationTool.getSelectedUrl("/page/contract/list/conexeclist.xhtml");
    }

    public void contractSelected() {
        mainInfo = mainInfoModel.getRowData();
        master.setNumber(mainInfo.getNumber());
        master.setConName(mainInfo.getCname());
        initContentModel(mainInfo.getNumber());
    }

    public void contentSelected_rows() {
        for (ContractContent row : contentModel) {
            if (row.isSelected()) {
                ConExecDetail existContentExec = contentIsExists(row.getId());
                if (existContentExec != null) {
                    /* 当前执行单中 已有 该合同内容 的执行条目,不可重复添加 */
                    System.out.println(" * * * " + "当前列表中已存在 contentId 为：" + row.getId() + "  * * * ");
                } else {
                    ConExecDetail temp = new ConExecDetail();
                    temp.setContentId(row.getId());
                    temp.setCode(row.getCode());
                    temp.setName(row.getName());
                    temp.setTechnicrequirment(row.getTecrequire());
                    temp.setPrice(row.getPrice());
                    temp.setCount(row.getCount());
                    temp.setTotal(row.getCount() * row.getPrice());
                    temp.setCountInCon(row.getCount());
                    detailli.add(temp);
                }
            }
        }
    }

    private ConExecDetail contentIsExists(Integer contentId) {
        for (ConExecDetail row : detailli) {
            if (contentId.intValue() == row.getContentId().intValue()) return row;
        }
        return null;
    }

    public void contentSelected() {
        ContractContent row = contentModel.getRowData();
        detailli.get(contentRowIndex).setContentId(row.getId());
        detailli.get(contentRowIndex).setCode(row.getCode());
        detailli.get(contentRowIndex).setName(row.getName());
        detailli.get(contentRowIndex).setTechnicrequirment(row.getTecrequire());
        detailli.get(contentRowIndex).setPrice(row.getPrice());
        detailli.get(contentRowIndex).setCount(row.getCount());
        detailli.get(contentRowIndex).setTotal(row.getCount() * row.getPrice());
        detailli.get(contentRowIndex).setCountInCon(row.getCount());
    }

    private void initContentModel(String contractNumber) {
        List<ContractContent> contentList = ContractImpl.queryContractContentListForExec(contractNumber);
        contentModel.setWrappedData(contentList);
    }

    private void initStatus() {
        addOrUpdate = false;
        editStatus = false;
    }

    public void add() {
        addOrUpdate = true;
        editStatus = true;
        master = new ConExecMaster();
        master.setVounumber(getMaxNumber());
        detailModel.setWrappedData(detailli = new ArrayList<ConExecDetail>());
    }

    public void update() {
        addOrUpdate = false;
        editStatus = true;
    }

    public void delete() {
        ConExecImpl.deleteMaster(master.getVounumber().trim());
        ConExecImpl.deleteDetail(master.getVounumber().trim());
        detailli = ConExecImpl.queryDetailByNumber(master.getVounumber().trim());
        detailModel.setWrappedData(detailli);
        initStatus();
        master = new ConExecMaster();
    }

    public void checkSave() {
        /*
         for (ConExecDetail row : detailModel) {
         if (row.getCount().doubleValue() > row.getCountInCon().doubleValue()) {
         ToolKit.msgs_err("执行数量不能大于合同内容中的数量");
         return;
         }
         }         
         */

        /*
         * 验证说明：执行总数 不能超过 合同内容中的数量；
         */
        save();
    }

    private void save() {
//        if (addOrUpdate) {
//            ConExecImpl.insertMaster(master);
//        } else if (!addOrUpdate) {
//            ConExecImpl.updateMaster(master);
//        }
//        ConExecImpl.deleteDetail(master.getVounumber().trim());
//        for (ConExecDetail d : detailModel) {
//            d.setVounumber(master.getVounumber());
//            ConExecImpl.insertDetail(d);
//        }

        master.setContype(master.getNumber().substring(0,1));
        boolean ok = ConExecImpl.save(master, (ArrayList<ConExecDetail>) detailModel.getWrappedData(), addOrUpdate);
        if (ok) {
            initStatus();
        } else {
            ToolKit.msgs_err("保存失败");
        }
    }

    public void cancel() {
        if (addOrUpdate) {
            master = new ConExecMaster();
            detailModel.setWrappedData(detailli = new ArrayList<ConExecDetail>());
        }
        initStatus();
    }

    public void addLine() {
        ConExecDetail c = new ConExecDetail();
        detailli.add(c);
        detailModel.setWrappedData(detailli);
    }

    public void delLine() {
        detailli.remove(detailModel.getRowIndex());
        detailModel.setWrappedData(detailli);
    }

    private String getMaxNumber() {
        return ConExecImpl.getMaxNumber();
    }

    public DataModel<ConExecDetail> getDetailModel() {
        return detailModel;
    }

    public void setDetailModel(DataModel<ConExecDetail> detailModel) {
        this.detailModel = detailModel;
    }

    public List<ConExecDetail> getDetailli() {
        return detailli;
    }

    public void setDetailli(List<ConExecDetail> detailli) {
        this.detailli = detailli;
    }

    public ConExecMaster getMaster() {
        return master;
    }

    public void setMaster(ConExecMaster master) {
        this.master = master;
    }

    public DataModel<MainInformation> getMainInfoModel() {
        if (!Null.check(contype)) {
            Map map = new HashMap();
            map.put("type", contype);
            List<MainInformation> templi = ContractImpl.queryContractListIsChecked(map);
            mainInfoModel.setWrappedData(templi);
        }
        return mainInfoModel;
    }

    public void setMainInfoModel(DataModel<MainInformation> mainInfoModel) {
        this.mainInfoModel = mainInfoModel;
    }

    public Integer getContentRowIndex() {
        return contentRowIndex;
    }

    public void setContentRowIndex(Integer contentRowIndex) {
        this.contentRowIndex = contentRowIndex;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }

    public boolean isQuerybut() {
        return querybut;
    }

    public void setQuerybut(boolean querybut) {
        this.querybut = querybut;
    }

    public DataModel<ConExecMaster> getExecModel() {
        return execModel;
    }

    public void setExecModel(DataModel<ConExecMaster> execModel) {
        this.execModel = execModel;
    }

    public DataModel<ContractContent> getContentModel() {
        System.out.println(" * * * " + "getContentModel " + "  * * * ");
        return contentModel;
    }

    public void setContentModel(DataModel<ContractContent> contentModel) {
        this.contentModel = contentModel;
    }

    public List<ConExecDetail> getDetailListForShow() {
        return detailListForShow;
    }

    public void setDetailListForShow(List<ConExecDetail> detailListForShow) {
        this.detailListForShow = detailListForShow;
    }

    public MainInformation getMainInfo() {
        return mainInfo;
    }

    public void setMainInfo(MainInformation mainInfo) {
        this.mainInfo = mainInfo;
    }

    public List<ConExecDetail> getAllExecList() {
        if (master != null && !master.getNumber().isEmpty()) {
            allExecList = initAllExecList(master.getNumber());
        }
        return allExecList;
    }

    private List<ConExecDetail> initAllExecList(String number) {
        return ConExecImpl.queryDetailByContractNumber(number);
    }

    public void setAllExecList(List<ConExecDetail> allExecList) {
        this.allExecList = allExecList;
    }

    public boolean isContractChanged() {
        return contractChanged;
    }

    public void setContractChanged(boolean contractChanged) {
        this.contractChanged = contractChanged;
    }

    public String getContype() {
        return contype;
    }

    public void setContype(String contype) {
        this.contype = contype;
    }
}
